<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.dao.${classInfo.className}Dao">

    <resultMap id="BaseResultMap" type="${packageName}.entity.base.${classInfo.className}DO" >
        <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
            <#list classInfo.fieldList as fieldItem >
                <result column="${fieldItem.columnName}" property="${fieldItem.fieldName}" />
            </#list>
        </#if>
    </resultMap>

    <sql id="${classInfo.className}_All_Columns">
        <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
            <#list classInfo.fieldList as fieldItem >
                ${classInfo.mybatisName}.${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
            </#list>
        </#if>
    </sql>



    <sql id="Base_Column_List">
        <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
            <#list classInfo.fieldList as fieldItem >
                ${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
            </#list>
        </#if>
    </sql>

    <sql id="Insert_Base_Columns">
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                    <#if fieldItem.columnName != "id" >
<#--
                        ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName} and ${classInfo.mybatisName}.${fieldItem.fieldName}!="" ${r"'>"}
-->
                        ${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
<#--
                        ${r"</if>"}
-->
                    </#if>
                </#list>
            </#if>
        </trim>
    </sql>

    <sql id="Insert_Base_Vlues">
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                    <#if fieldItem.columnName != "id" >
<#--
                        ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName} and ${classInfo.mybatisName}.${fieldItem.fieldName}!="" ${r"'>"}
-->
                        ${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName} }<#if fieldItem_has_next>,</#if>
<#--
                        ${r"</if>"}
-->
                    </#if>
                </#list>
            </#if>
        </trim>
    </sql>

    <!-- 插入数据 -->
    <insert id="insert">

        INSERT INTO ${classInfo.tableName}

        <include refid="Insert_Base_Columns"></include>
        VALUES
        <include refid="Insert_Base_Vlues"></include>

    </insert>

    <!-- 插入不为空的数据 -->
    <insert id="insertSelective">
        INSERT INTO ${classInfo.tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                    <#if fieldItem.columnName != "id" >
                          <#if fieldItem.fieldClass == 'String'>
                        ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName} and ${classInfo.mybatisName}.${fieldItem.fieldName}!="" ${r"'>"}
                        ${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
                        ${r"</if>"}
                         <#else>
                             ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName}  ${r"'>"}
                             ${fieldItem.columnName}<#if fieldItem_has_next>,</#if>
                             ${r"</if>"}
                          </#if>
                    </#if>
                </#list>
            </#if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                    <#if fieldItem.columnName != "id" >
                        <#if fieldItem.fieldClass == 'String'>
                        ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName} and ${classInfo.mybatisName}.${fieldItem.fieldName}!="" ${r"'>"}
                        ${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName} }<#if fieldItem_has_next>,</#if>
                        ${r"</if>"}
                        <#else>
                            ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName}  ${r"'>"}
                            ${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName} }<#if fieldItem_has_next>,</#if>
                            ${r"</if>"}
                        </#if>
                    </#if>
                </#list>
            </#if>
        </trim>
    </insert>

    <!-- 批量插入数据 -->
    <insert id="batchInsert">

        INSERT INTO ${classInfo.tableName}

        <include refid="Insert_Base_Columns"></include>
        VALUES
        <foreach collection="iterable" item="${classInfo.mybatisName}"  separator="," >

            <include refid="Insert_Base_Vlues"></include>

        </foreach>

    </insert>

    <!-- mapped statement for BasAccountDAO.query -->
    <!-- 根据输入条件查询数据 -->
    <select id="query" resultMap="BaseResultMap">

        SELECT
        <include refid="${classInfo.className}_All_Columns"/>
        FROM ${classInfo.tableName} ${classInfo.mybatisName}
        <where>

            ${classInfo.mybatisName}.is_deleted=0

            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >

                    <#if fieldItem.fieldClass == 'String'>
                        ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName} and ${classInfo.mybatisName}.${fieldItem.fieldName}!="" ${r"'>"}
                        and ${classInfo.mybatisName}.${fieldItem.columnName} =${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName} }
                        ${r"</if>"}
                    <#else>
                        ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName} ${r"'>"}
                        and ${classInfo.mybatisName}.${fieldItem.columnName} =${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName} }
                        ${r"</if>"}
                    </#if>

                </#list>
            </#if>
            <if test="${classInfo.mybatisName}.ew != null">
                <include refid="Common.ew"/>
            </if>
        </where>
        <if test="${classInfo.mybatisName}.orderBy !=null and orderBy !=''">
            <include refid="Common.orderBy"/>
        </if>
    </select>
    <!-- 根据输入条件不分页查询数据 -->
    <select id="queryList" resultMap="BaseResultMap">

        SELECT
        <include refid="${classInfo.className}_All_Columns"/>
        FROM ${classInfo.tableName} ${classInfo.mybatisName}
        <where>
            ${classInfo.mybatisName}.is_deleted=0
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                     <#if fieldItem.fieldClass == 'String'>
                         ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName} and ${classInfo.mybatisName}.${fieldItem.fieldName}!="" ${r"'>"}
                         and ${classInfo.mybatisName}.${fieldItem.columnName} =${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName} }
                         ${r"</if>"}
                     <#else>
                         ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName}  ${r"'>"}
                         and ${classInfo.mybatisName}.${fieldItem.columnName} =${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName} }
                         ${r"</if>"}
                     </#if>
                </#list>
            </#if>
        </where>
    </select>


    <!-- 根据主键修改数据 -->
    <update id="update">

        UPDATE ${classInfo.tableName}
        <set>
            <#list classInfo.fieldList as fieldItem >
                ${fieldItem.columnName} = ${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName}${r"}"}<#if fieldItem_has_next>,</#if>
            </#list>
        </set>
        <where>
            <!-- 主键ID -->
             id = ${r"#{"}${classInfo.mybatisName}.id${r"}"}
        </where>

    </update>

    <!-- 根据主键修改不为空的数据 -->
    <update id="updateSelective">

        UPDATE ${classInfo.tableName}
        <set>
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                    <#if fieldItem.fieldClass == 'String'>
                        ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName} and ${classInfo.mybatisName}.${fieldItem.fieldName}!="" ${r"'>"}
                        ${fieldItem.columnName} =${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName} }<#if fieldItem_has_next>,</#if>
                        ${r"</if>"}
                    <#else>
                        ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName}  ${r"'>"}
                        ${fieldItem.columnName} =${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName} }<#if fieldItem_has_next>,</#if>
                        ${r"</if>"}
                    </#if>
                </#list>
            </#if>
        </set>
        <where>
            <!-- 主键ID -->
             id = ${r"#{"}${classInfo.mybatisName}.id${r"}"}
        </where>

    </update>

    <!-- 根据主键删除数据(逻辑删除) -->
    <update id="delete">

        UPDATE ${classInfo.tableName}
        set is_deleted=1
        <where>
            <!-- 主键ID -->
            AND id = ${r"#{"}${classInfo.mybatisName}.id${r"}"}
        </where>

    </update>

    <!-- 根据指定字段的值查询数据 -->
    <select id="findById" resultMap="BaseResultMap">

        SELECT
        <include refid="${classInfo.className}_All_Columns"/>
        FROM ${classInfo.tableName} ${classInfo.className}
        <where>
            is_deleted=0
            AND id = ${r"#{"}id${r"}"}
        </where>

    </select>

    <!-- 根据指定字段的多个值查询数据 -->
    <select id="findByIdIn" resultMap="BaseResultMap">

        SELECT
        <include refid="${classInfo.className}_All_Columns"/>
        FROM ${classInfo.tableName} ${classInfo.className}
        <where>
            is_deleted=0
            <!-- 主键ID -->
           and  id IN
            <foreach collection="iterable" item="item"  open="(" separator="," close=")">
                ${r"#{"}item${r"}"}
            </foreach>
        </where>
        ORDER BY id DESC

    </select>




    <update id="batchUpdate">
        update ${classInfo.tableName}
        <trim prefix="set" suffixOverrides=",">
        <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
            <#list classInfo.fieldList as fieldItem >
                <trim prefix="${fieldItem.columnName}=case" suffix="end,">
                    <foreach collection="iterable" item="${classInfo.mybatisName}">
                         <#if fieldItem.fieldClass == 'String'>
                        ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName} and ${classInfo.mybatisName}.${fieldItem.fieldName}!="" ${r"'>"}
                        when id=${r"#{"}${classInfo.mybatisName}.id${r"}"} then ${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName}${r"}"}
                        ${r"</if>"}
                         <#else>
                             ${r"<if test ='null != "} ${classInfo.mybatisName}.${fieldItem.fieldName}  ${r"'>"}
                             when id=${r"#{"}${classInfo.mybatisName}.id${r"}"} then ${r"#{"}${classInfo.mybatisName}.${fieldItem.fieldName}${r"}"}
                             ${r"</if>"}
                         </#if>
                    </foreach>
                </trim>
            </#list>
        </#if>
        </trim>
                <where>
                    id in
                    <foreach collection="iterable" item="${classInfo.mybatisName}" open="(" separator="," close=")">
                        ${r"#{"}${classInfo.mybatisName}.id${r"}"}
                    </foreach>
                </where>

    </update>

</mapper>
